<template>
  <div class="order-confirmation">
    <h2>订单确认</h2>
    <vue-masonry :cols="{ default: 4, 1100: 3, 700: 2, 500: 1 }" gutter="30px">
      <div v-for="item in orderDetails" :key="item.id" class="order-item">
        <img :src="item.imageUrl" alt="菜品图片" />
        <div class="item-name">{{ item.id }}</div>
        <el-input v-model="item.orderRemark" placeholder="备注" type="textarea" rows="2" class="item-remark"></el-input>
      </div>
    </vue-masonry>
    <div class="button-group">
      <el-button type="primary" class="confirm-button" @click="confirmOrder">确认下单</el-button>
      <el-button type="primary" class="return-button" @click="goToHome">返回首页</el-button>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { VueMasonry } from 'vue-masonry-css';

export default {
  name: 'OrderConfirmation',
  components: {
    VueMasonry
  },
  data() {
    return {
      orderDetails: []
    };
  },
  created() {
    this.loadOrderDetails();
  },
  methods: {
    goToHome() {
      this.$router.push('/dashboard/menu-management');
    },
    confirmOrder() {
      const menuIds = this.orderDetails.map(item => item.id).join(',');
      const orderRemarks = this.orderDetails.map(item => item.orderRemark || '').join('|'); // 用 '|' 分隔各个备注
      console.log('menuIds', menuIds);
      axios.get('/orders/placeOrder', {
        params: {
          menuIds: menuIds,
          orderRemark: orderRemarks // 添加订单备注字段
        }
      })
          .then(response => {
            console.log(response.data);
            if (response.data.msg === "订单创建成功") {
              this.$message.success('下单成功！');
              this.$router.push('/dashboard/menu-management');
            } else {
              this.$message.error(response.data);
            }
          })
          .catch(error => {
            console.error('Error placing order:', error);
            this.$message.error('下单失败');
          });
    },
    loadOrderDetails() {
      const details = this.$route.query.selectedItems;
      if (details) {
        try {
          const selectedItems = JSON.parse(details);
          const menuIds = selectedItems.map(item => item.menuId);
          const menuIdsString = menuIds.join(','); // 将 menuIds 转化为逗号分隔的字符串
          this.fetchMenuDetails(menuIdsString);
        } catch (e) {
          console.error('Error parsing order details:', e);
        }
      }
    },
    fetchMenuDetails(menuIdsString) {
      axios.get('/menu/batchGet', {
        params: {
          ids: menuIdsString
        }
      })
          .then(response => {
            if (response.data) {
              this.orderDetails = response.data.map(item => ({ ...item, orderRemark: '' })); // 初始化备注为空字符串
              console.log('this.orderDetails', this.orderDetails);
            } else {
              this.$message.error('获取订单详情失败');
            }
          })
          .catch(error => {
            console.error('Error fetching menu details:', error);
            this.$message.error('获取订单详情失败');
          });
    }
  }
};
</script>

<style scoped>
.order-confirmation {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

h2 {
  margin-bottom: 20px;
}

.order-details {
  width: 100%;
  margin-bottom: 30px;
}

.order-item {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.order-item img {
  border-radius: 10px;
  height: 200px;
  object-fit: cover;
  width: 100%;
}

.item-name {
  font-weight: bold;
  margin-top: 15px;
}

.item-remark {
  width: 100%;
  margin-top: 10px;
}

.button-group {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
  width: 100%;
  max-width: 400px;
}

.confirm-button,
.return-button {
  flex: 1;
  margin: 0 10px;
}
</style>
